package com.itlong.cloud.controller.settings;

import com.itlong.cloud.POJO.DTO.property.PropertySMSAccountInfoDTO;
import com.itlong.cloud.POJO.DTO.property.PropertySMSOverviewDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertySMSAccountInfoVO;
import com.itlong.cloud.POJO.VO.property.PropertySMSOverviewVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.PropertyErrorCodeEnum;
import com.itlong.cloud.noncore.property.IPropertySMSOverviewServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      短信概览 管理控制器
 * </desc>
 *
 * @createDate 2017-10-19
 */
@RestController
@RequestMapping("settings/smsOverview")
public class PropertySMSOverviewController extends BaseController {

    @Autowired
    IPropertySMSOverviewServiceClient iPropertySMSOverviewServiceClient;


    /**
     * <desc>
     *      查询 短信概览
     * <desc/>
     *
     * @param pageDTO 查询短信概览 数据传输对象
     * @return 短信概览 返回对象
     * @author ZiXuan
     * @createDate 2017-10-19
     */
    @RequestMapping(path = "/getPage", method = RequestMethod.POST)
    @ResponseBody
    public Object getPage(PropertySMSOverviewDTO pageDTO) {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pageDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertySMSOverviewDTO.class, request);
            if (null == pageDTO) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }

            Page<PropertySMSOverviewVO> page = iPropertySMSOverviewServiceClient.getPage(pageDTO);

            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), page);
        } catch (Exception e) {
            throw new DataAccessException("【物业_系统设置_短信概览】分页查询失败", e);
        }
    }

    /**
     * <desc>
     *      短信统计
     * <desc/>
     *
     * @param pageDTO 信息DTO
     * @return
     * @author ZiXuan
     * @createDate 2017-10-23
     */
    @RequestMapping(path = "/getSMSAccountInfo", method = RequestMethod.POST)
    @ResponseBody
    public Object getSMSAccountInfo(PropertySMSAccountInfoDTO pageDTO) {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pageDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertySMSAccountInfoDTO.class, request);
            if (null == pageDTO) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }

            //项目编号
            if(StringUtils.isBlank(pageDTO.getProjectId())){
                return new MessageVO(PropertyErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
            }

            PropertySMSAccountInfoVO data = iPropertySMSOverviewServiceClient.getSMSAccountInfo(pageDTO);

            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), data);

        } catch (Exception e) {
            throw new DataAccessException("【物业_系统设置_短信概览】查询短信统计信息失败", e);
        }
    }
}
